from django.db import models
from django.contrib.auth.models import User


class ChatSession(models.Model):
    """AI对话会话表"""
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    session_id = models.CharField(
        max_length=100, 
        unique=True, 
        verbose_name='会话ID',
        db_comment='唯一会话标识'
    )
    start_time = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='开始时间',
        db_comment='会话开始时间'
    )
    end_time = models.DateTimeField(
        null=True, 
        blank=True, 
        verbose_name='结束时间',
        db_comment='会话结束时间'
    )
    emotion_summary = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='情绪总结',
        db_comment='本次会话情绪分析结果JSON'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否活跃',
        db_comment='会话是否活跃状态'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_chat_session'
        db_table_comment = 'AI对话会话表'
        verbose_name = 'AI对话会话'
        verbose_name_plural = 'AI对话会话'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.session_id}'


class ChatMessage(models.Model):
    """AI对话消息表"""
    ROLE_CHOICES = [
        ('user', '用户'),
        ('assistant', 'AI助手'),
        ('system', '系统'),
    ]
    
    session = models.ForeignKey(
        ChatSession, 
        on_delete=models.CASCADE, 
        related_name='messages',
        verbose_name='会话',
        db_comment='关联会话ID'
    )
    role = models.CharField(
        max_length=20, 
        choices=ROLE_CHOICES, 
        verbose_name='角色',
        db_comment='消息角色类型'
    )
    content = models.TextField(
        verbose_name='消息内容',
        db_comment='对话消息文本内容'
    )
    emotion_type = models.CharField(
        max_length=50, 
        null=True, 
        blank=True, 
        verbose_name='情绪类型',
        db_comment='识别的情绪类型(焦虑/抑郁/愤怒/悲伤/快乐等)'
    )
    emotion_intensity = models.FloatField(
        null=True, 
        blank=True, 
        verbose_name='情绪强度',
        db_comment='情绪强度评分0-1'
    )
    timestamp = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='消息时间',
        db_comment='消息发送时间'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_chat_message'
        db_table_comment = 'AI对话消息表'
        verbose_name = 'AI对话消息'
        verbose_name_plural = 'AI对话消息'
        ordering = ['timestamp']

    def __str__(self):
        return f'{self.session.session_id} - {self.role} - {self.timestamp}'


class EmotionAnalysis(models.Model):
    """多模态情绪分析表"""
    MODALITY_CHOICES = [
        ('text', '文字'),
        ('voice', '语音'),
        ('image', '图像'),
        ('multi', '多模态融合'),
    ]
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    message = models.ForeignKey(
        ChatMessage, 
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name='关联消息',
        db_comment='关联的对话消息ID'
    )
    modality = models.CharField(
        max_length=20, 
        choices=MODALITY_CHOICES, 
        verbose_name='分析模态',
        db_comment='情绪分析的输入模态'
    )
    emotion_result = models.JSONField(
        verbose_name='情绪结果',
        db_comment='情绪分析详细结果JSON'
    )
    confidence = models.FloatField(
        verbose_name='置信度',
        db_comment='分析结果置信度0-1'
    )
    analysis_time = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='分析时间',
        db_comment='情绪分析时间'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_emotion_analysis'
        db_table_comment = '多模态情绪分析表'
        verbose_name = '情绪分析'
        verbose_name_plural = '情绪分析'
        ordering = ['-analysis_time']

    def __str__(self):
        return f'{self.user.username} - {self.modality} - {self.analysis_time}'


class UserProfile(models.Model):
    """用户心理档案表"""
    user = models.OneToOneField(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    age = models.IntegerField(
        null=True, 
        blank=True, 
        verbose_name='年龄',
        db_comment='用户年龄'
    )
    gender = models.CharField(
        max_length=10, 
        null=True, 
        blank=True, 
        verbose_name='性别',
        db_comment='用户性别'
    )
    occupation = models.CharField(
        max_length=100, 
        null=True, 
        blank=True, 
        verbose_name='职业',
        db_comment='用户职业'
    )
    interests = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='兴趣爱好',
        db_comment='用户兴趣爱好描述'
    )
    psychological_history = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='心理史',
        db_comment='既往心理问题记录'
    )
    preferences = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='个人偏好',
        db_comment='用户偏好设置JSON'
    )
    privacy_level = models.IntegerField(
        default=3, 
        verbose_name='隐私级别',
        db_comment='隐私设置级别1-5'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_user_profile'
        db_table_comment = '用户心理档案表'
        verbose_name = '用户心理档案'
        verbose_name_plural = '用户心理档案'

    def __str__(self):
        return f'{self.user.username}的心理档案'
